<?php

namespace app\admin\model\tongcheng;

use addons\tongcheng\enum\DeliveryRefundStatus;
use addons\tongcheng\model\BaseModel;


class DeliveryOrderRefundModel extends BaseModel
{


    // 表名
    protected $name = 'tongcheng_delivery_order_refund';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'user_role_text',
        'operate_role_text',
        'status_text',
        'buttons',
        'createtime_text',
    ];


    public function getUserRoleList()
    {
        return ['CUSTOMER' => __('Customer'), 'RIDER' => __('Rider'), 'ADMIN' => __('Admin'), 'SYSTEM' => __('System')];
    }

    public function getOperateRoleList()
    {
        return ['CUSTOMER' => __('Customer'), 'RIDER' => __('Rider'), 'ADMIN' => __('Admin'), 'SYSTEM' => __('System')];
    }

    public function getStatusList()
    {
        return [
            DeliveryRefundStatus::WAIT     => '待处理',
            DeliveryRefundStatus::ACCEPT   => '已同意退款',
            DeliveryRefundStatus::REJECT   => '已驳回退款',
            DeliveryRefundStatus::UNHANDLE => '退款待处理',
        ];
    }


    public function getUserRoleTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['user_role']) ? $data['user_role'] : '');
        $list  = $this->getUserRoleList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOperateRoleTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['operate_role']) ? $data['operate_role'] : '');
        $list  = $this->getOperateRoleList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list  = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getButtonsAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        return $this->getStatus($value, 'buttons');
    }

    /**
     * 获取配送订单退款状态
     * @param $status
     * @param $type
     * @return array|mixed|string[]|void
     */
    protected function getStatus($status, $type)
    {
        if ($type == 'text') {
            $statusList = $this->getStatusList();
            return $statusList[$status];
        } else if ($type == 'buttons') {
            switch ($status) {
                case DeliveryRefundStatus::UNHANDLE:
                    return [
                        'refund_accept' => '同意退款',
                        'refund_reject' => '拒绝退款',
                    ];

                default:
                    break;
                // return [];
            }
        }
    }


    public function zone()
    {
        return $this->belongsTo('Zone', 'zone_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function order()
    {
        return $this->belongsTo('app\admin\model\tongcheng\delivery\Order', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function coupon()
    {
        return $this->belongsTo('Coupon', 'refund_coupon_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
